<!doctype html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta content="always" name="referrer">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <#include "../common/header.html">
</head>
<body>
<!--最外面的灰底框,用于与ifram区别开-->
<div class="col-xs-12 lightgray_div">
    <!--白底框-->
    <div class="col-xs-12 white_div">
        <!--搜索框-->
        <div class="col-xs-12 search_div">
            <div class="input-group g-fr">
                <form id="inquire" class="form-inline ">
                    <div class="input-group ">
                        <select class="form-control" name="type" id="type">
                            <option value="0">所有类型</option>
                            <option value="1">普通会员</option>
                            <option value="2">年卡会员</option>
                            <option value="3">终身会员</option>
                        </select>
                    </div>

                    <div class="input-group ">
                        <input type="text" class="form-control" name="name_or_number" placeholder="名称/卡号">
                    </div>
                    <button type="button" class="layui-btn" style="height: 30px" id="btn_search" onclick="table_func.search()"><i
                            class="layui-icon layui-icon-search"></i></button>
                </form>
            </div>
        </div>
        <div class="col-xs-12">
            <div id="table_parent_div" >
                <table id="table_div"  lay-filter="main"></table>
            </div>
        </div>
    </div>
</div>
</body>
<#include "../common/layui_init.html">
<!--表格工具类的按钮-->
<script type="text/html" id="toolbarBox">
    <div style="margin-bottom: -5px">
        <div class="layui-btn-container " style="float: left;">
            <button class="layui-btn " onclick="table_func.add()"><i class="layui-icon layui-icon-add-1"></i>添加</button>
            <button class="layui-btn  layui-btn-normal" onclick="table_func.edit()"><i
                    class="layui-icon layui-icon-edit"></i>编辑
            </button>
            <button class="layui-btn   layui-btn-danger" onclick="table_func.delete()"><i class="layui-icon layui-icon-delete"></i>删除
            </button>
            <button class="layui-btn   layui-btn-normal" onclick="table_func.create_user()"><i class="layui-icon layui-icon-add-circle"></i>创建并绑定用户
            </button>
            <button class="layui-btn   layui-btn-normal" onclick="table_func.relieve_user()"><i class="layui-icon layui-icon-subtraction"></i>解绑用户
            </button>
            <button class="layui-btn   layui-btn-normal" onclick="table_func.bind_user()"><i class="layui-icon layui-icon-addition"></i>绑定用户
            </button>
        </div>
    </div>
</script>
<script type="text/javascript">
    //表头
    var card_map={
        "1":"普通会员",
        "2":"年卡会员",
        "3":"终身会员"
    }
    var cols = [[
        {field: 'id', title: 'id', sort: true, type: 'checkbox', fixed: 'left'}
        , {field: 'id', title: 'ID'}
        , {field: 'name', title: '会员名称'}
        , {field: 'card_number', title: '会员卡号'}
        , {
            field: 'type', title: '会员类型'
            , templet: function (col) {
                return common_utils.transform_map(col.type,card_map);

            }
        }
        , {field: 'sex_value', title: '性别'}
        , {
            field: 'expiration_time', title: '会员到期时间'
            , templet: function (col) {
                return layui_table.date_format(col.expiration_time, "yyyy-MM-dd")
            }
        }
        , {field: 'balance', title: '余额'}
        , {field: 'phone', title: '手机号'}
        , {field: 'integral', title: '积分'}
        , {field: 'addr', title: '家庭住址'}
        , {field: 'personal_trainer_num', title: '私教次数'}
        , {
            field: 'birthdate', title: '出生日期'
            , templet: function (col) {
                return layui_table.date_format(col.birthdate, "yyyy-MM-dd")
            }
        }
        , {field: 'user_value', title: '用户'}
        , {
            field: 'status', title: '状态'
            , templet: function (col) {
                return common_utils.status_transform(col.status)
            }
        }
        , {field: 'remark', title: '备注'}
        , {
            field: 'gmt_create', title: '创建时间'
            , templet: function (col) {
                return layui_table.date_format(col.gmt_create, "yyyy-MM-dd hh:mm:ss")
            }
        }
        ,{
            field: 'gmt_create', title: '修改时间'
            , templet: function (col) {
                return layui_table.date_format(col.gmt_modified, "yyyy-MM-dd hh:mm:ss")
            }
        }
    ]]
    var select_user_id=null;
    //页面要用的方法
    var table_func = {
        //表格初始化
        init: function () {
            //设置表格渲染的参数
            layui_table.set_table_data('${request.contextPath}/jsf_member/query', cols, "#toolbarBox");
            //进行表格渲染
            layui_table.table_render();
        },
        //搜索
        search: function () {
            layui_table.search(tool.paraArrayToLiteral($("#inquire").serializeArray()))
        },
        //添加
        add: function () {
            window.location.href = "${request.contextPath}/pages/member/add";
        },
        //编辑
        edit: function () {
            var id = layui_table.selectOne();
            if (id != null) {
                window.location.href = "${request.contextPath}/pages/member/add?id=" + id;
            }
        },
        //解绑用户
        relieve_user: function () {
            var id = layui_table.selectOne();
            if (id != null) {
                $.post("${request.contextPath}/jsf_member/setMemberUserId", {id:id,user_id:""}, function (o) {
                    if (o.code == 0) {
                        layer.msg("用户解绑成功!");
                        table_func.search();
                    } else {
                        layui_layer.error_alert(o.message);
                    }
                })
            }
        },

        //绑定用户
        bind_user: function () {
            var id = layui_table.selectOne();
            if (id != null) {
                layer.open({
                    type: 2,
                    title: '绑定账号',   //标题
                    area: ['90%', '90%'],   //宽高
                    shade: 0.4,   //遮罩透明度
                    scrollbar: false,
                    content:"/admin/pages/user/module/select_user?id=" + id,
                    end: function(){
                        if(!tool.isEmpty(select_user_id)){
                            $.post("${request.contextPath}/jsf_member/setMemberUserId", {id:id,user_id:select_user_id}, function (o) {
                                if (o.code == 0) {
                                    layui_layer.ajax_verify(o)
                                    table_func.search();
                                } else {
                                    layui_layer.error_alert(o.message);
                                }
                            })
                        }
                        select_user_id=null;
                    }
                });
            }
        },

        //创建用户
        create_user: function () {
            var id = layui_table.selectOne();
            var data=layui_table.get_checked_data();
            if (id != null) {
                layer.open({
                    type: 2,
                    title: '绑定账号',   //标题
                    area: ['600px', '400px'],   //宽高
                    shade: 0.4,   //遮罩透明度
                    scrollbar: false,
                    content:"${request.contextPath}/pages/member/create_user?name=" + data[0].name +"&card_number="+ data[0].card_number+"&member_id="+id,
                    end:function () {
                        table_func.search();
                    }
                });

            }
        },
        //删除
        delete: function () {
            var id = layui_table.selectMany();
            if (id != null) {
                layui_layer.confirm("确定要删除吗?",function () {
                    $.post("${request.contextPath}/jsf_member/delete", {ids:tool.asString(id)}, function (o) {
                        if (o.code == 0) {
                            layer.msg(o.message);
                            table_func.search();
                        } else {
                            layui_layer.error_alert(o.message);
                        }
                    });
                })
            }
        }
    }
    $(function () {
        //设置表格的属性值
        layui_table.lay_attr = $("#table_div").attr("lay-filter");
        //设置点击表格行,进行选中
        layui_table.click_line_checked();
        $("#type").select2();
        table_func.init();
    });
</script>
<#include "../common/footer-inner.html">
</html>
